// pages/myself/merchant/UpgradeVIP.js
let network = null;
let utils = null;
let api = null;
const app = getApp();
var Zan = require('../../../dist/index.js');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');

Page(Object.assign({}, Zan.TopTips, {

  /**
   * 页面的初始数据
   */
  data: {
    updateObj: [],
    brandId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    this.setData({
      brandId: options.brandId
    })

    network._post_json(this, api.updateVip, {}, success);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  pay: function (e) {
    let item = e.currentTarget.dataset.item
    // 订单类型type  1=开通云商  2=开通来店  3=开通商圈  4=开通小游戏  5=提交工单
    if (item.pk == "upEcVip") {
      network._post_json(this, api.pay, { brandId: this.data.brandId, type: 1, total: item.pv, /**path: this.data.imgUrl, name: this.data.name, brandType: this.data.typeId, code: this.data.code,*/ totalKey: item.pk }, paySuccess);
    } else if (item.pk == "upFwVip") {
      network._post_json(this, api.pay, { brandId: this.data.brandId, type: 2, total: item.pv, /**path: this.data.imgUrl, name: this.data.name, brandType: this.data.typeId, code: this.data.code,*/ totalKey: item.pk }, paySuccess);
    }
  }
}))

/**
 * 获取升级费用成功
 */
var success = function (that, res) {
  if (res.data.data) {
    that.setData({
      updateObj: res.data.data
    })
  }
}

// 创建订单
var paySuccess = function (that, res) {
  if (res.data.data) {
    let order = res.data.data;
    network._post_json(that, api.payPre, { appId: app.globalData.appId, orderNo: order.orderNo, businessAppId: app.globalData.businessAppId, OrderDesc: order.orderDesc, totalFee: order.total }, payPreSuccess);
  }
}

// 生成预支付订单
var payPreSuccess = function (that, res) {
  if (res.data.data) {
    // 支付
    wx.requestPayment({
      timeStamp: res.data.data.timeStamp,
      nonceStr: res.data.data.nonceStr,
      package: res.data.data.package,
      signType: res.data.data.signType,
      paySign: res.data.data.paySign,
      success: function (res) {
        WxNotificationCenter.postNotificationName("RefreshMerchantBrand");
        wx.navigateBack();
      },
      fail: function (res) {

      }
    });
  }
}